package com.leo.interceptor;


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: HandlerInterceptor4
 * @Description: 继承HandlerInterceptorAdapter实现拦截器
 * @Author: leo825
 * @Date: 2020-02-08 20:36
 * @Version: 1.0
 */
public class HandlerInterceptor4 extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("HandlerInterceptor4 preHandle");
        HttpSession session = request.getSession();
        session.setAttribute("startTime",System.currentTimeMillis());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("HandlerInterceptor4 postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HttpSession session = request.getSession();
        System.out.println("HandlerInterceptor4 afterCompletion");
        long startTime = (long)session.getAttribute("startTime");
        System.out.println("HandlerInterceptor4 过滤的接口耗时：" + (System.currentTimeMillis() - startTime) + "ms");
    }
}
